package com.lanxin.oa.qx.controller;

import com.lanxin.bean.*;
import com.lanxin.oa.qx.service.QxroleService;
import com.lanxin.util.PageBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpSession;
import java.util.List;

@Controller
@RequestMapping("qx")
public class QxroleController {

    @Autowired
    private QxroleService qxroleService;

    //查询角色表
    @RequestMapping("selectRole")
    public ModelAndView selectRole(Integer page,Integer pageSize){
        PageBean<QxRole> listRole = qxroleService.selectRole(page,pageSize);
        ModelAndView mv = new ModelAndView();
        mv.addObject("listRole",listRole);
        mv.setViewName("../view/qx/listRole.jsp");
        return mv;
    }

    //添加角色
    @RequestMapping("eidsRole")
    public ModelAndView eidsRole(QxRole role){
        qxroleService.eidsRole(role);
        ModelAndView mv = new ModelAndView();
        mv.setViewName("redirect:selectRole");
        return mv;
    }

    //通过id查询
    @RequestMapping("selectById")
    @ResponseBody
    public QxRole selectById(Integer oid){
        return qxroleService.seletcById(oid);
    }

    //修改角色
    @RequestMapping("updateRole")
    public ModelAndView updateRole(QxRole role){
        qxroleService.updateRole(role);
        ModelAndView mv = new ModelAndView();
        mv.setViewName("redirect:selectRole");
        return mv;
    }

    //通过条件查询
    @RequestMapping("pageRole")
    public ModelAndView pageRole(Integer page,Integer pageSize,String roleName){
        PageBean<QxRole> listRole = qxroleService.pageRole(page,pageSize,roleName);
        ModelAndView mv = new ModelAndView();
        mv.addObject("listRole",listRole);
        mv.setViewName("view/qx/listRole");
        return mv;
    }

    //查询第三方表
    @RequestMapping("selectRoleUser")
    public ModelAndView selectRoleUser(Integer oid){
        QxRole selectRoleUser = qxroleService.selectRoleUser(oid);
        ModelAndView mv = new ModelAndView();
        mv.addObject("selectRoleUser",selectRoleUser);
        mv.setViewName("../view/qx/qxroleUser.jsp");
        return mv;
    }

    //添加第三方表
    @RequestMapping("insertRoleUser")
    public ModelAndView insertRoleUser(Integer roid,String eids){
        System.out.println(roid+"  "+eids);
        qxroleService.insertRoleUser(roid,eids);
        ModelAndView mv = new ModelAndView();
        mv.setViewName("redirect:selectRole");
        return mv;
    }

    //查询第三方表权限表
    @RequestMapping("listFunctions")
    public ModelAndView listFunctions(Integer oid){
        List<QxFunction> functions = qxroleService.listFunctions();
        QxRole selectRoleUser = qxroleService.selectRoleUser(oid);
        ModelAndView mv = new ModelAndView();
        mv.addObject("functions",functions);
        mv.addObject("selectRoleUser",selectRoleUser);
        mv.setViewName("../view/qx/qxroleFunction.jsp");
        return mv;
    }

    //添加第三方表权限表
    @RequestMapping("insertFunction")
    public ModelAndView insertFunction(Integer oid,String fids){
        qxroleService.insertRoleFunction(oid, fids);
        ModelAndView mv = new ModelAndView();
        mv.setViewName("redirect:selectRole");
        return mv;
    }

    //查询我的权限
    @RequestMapping("myFunctions")
    public ModelAndView myFunctions(HttpSession session){
        ZzAccount account = (ZzAccount) session.getAttribute("account");
        List<ZzEmployee> functionList = qxroleService.myFunctions(account.getEmployee().getOid());
        ModelAndView mv = new ModelAndView();
        mv.addObject("functionList",functionList);
        mv.setViewName("../view/qx/myFunction.jsp");
        return mv;
    }

    @RequestMapping("deletecard")
    @ResponseBody
    public boolean deletecard(QxRole role){
        return qxroleService.deleteRole(role);
    }

}
